package com.fingard.dsp.bank.directbank.vbao02;

import com.alibaba.fastjson.JSONObject;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.directbank.vbao02.cust.request.SinglePayRecQueryRequest;
import com.fingard.dsp.bank.directbank.vbao02.cust.request.SinglePayRecRequest;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingleBat;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingleBat;
import com.fingard.dsp.bank.reqrespobj.single.RetAtsSingle;
import com.fingard.dsp.bank.reqrespobj.single.RetAtsSingleBat;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;

public class VBAO02SinglePay extends VBAO02Base implements ISinglePay {
	public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
		RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
		RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();
		
		try {
			ActSetItem actItem = getBankConfig().getActSetItem(p_req1908.singleDetail.payAct, "");
			if (actItem == null) {
				tmpRetDetail.respCode = "0";
				tmpRetDetail.respInfo = "未找到账号:[" + p_req1908.singleDetail.payAct + "]商户配置信息!";
				tmpRetDetail.reqSeqID = p_req1908.singleDetail.askNumber;
				tmpRetDetail.transState = PaymentState.FAIL_Payment;
				tmpRetDetail.payInfoCode = FailCode.E9981;
				retAts.writeRetDetail(tmpRetDetail);
				return retAts;
			}
			
			SinglePayRecRequest singlePayRequest = new SinglePayRecRequest();

			singlePayRequest.setVersion("V5.1");
			singlePayRequest.setInputCharset(getCharset());
			singlePayRequest.setProductNo(getBankConfig().getMapValueByKey("productNoMapping", "singlePay"));
			singlePayRequest.setCustSeq(p_req1908.singleDetail.askNumber);
			singlePayRequest.setOrderAmount(p_req1908.singleDetail.payAmount);
			
			String isPrivate = p_req1908.singleDetail.isPrivate;
			if ("1".equals(isPrivate)) {
				isPrivate = "0";
			} else {
				isPrivate = "1";
			}
			singlePayRequest.setAccType(isPrivate);
			
			String cardType = p_req1908.singleDetail.cardType;
			if ("3".equals(cardType) || "4".equals(cardType)) {
				cardType = "2";
			} else if ("1".equals(cardType)) {
				cardType = "1";
			} else {
				cardType = "0";
			}
			singlePayRequest.setBankCardType(cardType);
			singlePayRequest.setBankCode(getBankConfig().getMapValueByKey("BankCodeMapping", p_req1908.singleDetail.recBankCode));
			singlePayRequest.setBankCardNo(p_req1908.singleDetail.recAct);
			singlePayRequest.setBankCardName(p_req1908.singleDetail.recName);
			singlePayRequest.setBankLineCode(p_req1908.singleDetail.cnapsCode);
			singlePayRequest.setBankLineName(p_req1908.singleDetail.cnapsName);
			singlePayRequest.setCertType(convertCertTypeToBank(p_req1908.singleDetail.certType));
			singlePayRequest.setCertId(p_req1908.singleDetail.certNum);
			singlePayRequest.setMobile(p_req1908.singleDetail.cellPhone);
			singlePayRequest.setPurpose(p_req1908.singleDetail.purpose);
			singlePayRequest.setMemo(p_req1908.singleDetail.memo);
			singlePayRequest.setDescription(p_req1908.singleDetail.abstractATS);
			
			JSONObject tmpRespJson = sendToBank(JSONObject.toJSONString(singlePayRequest), actItem, bankFront.getUrl("singleRecPay"));
			
			JSONObject tmpPlainJson = new JSONObject();
			
			tmpRetDetail.respCode = "0";
            if ("RSA".equals(tmpRespJson.get("encryptType"))) {
            	tmpPlainJson = decryptByRSA(tmpRespJson, actItem);
				tmpRetDetail.respInfo = tmpPlainJson.getString("returnMessage");
				tmpRetDetail.transAmt = tmpPlainJson.getString("orderAmount");
            } else {
            	tmpPlainJson = JSONObject.parseObject(tmpRespJson.getString("reqmsg"));
				tmpRetDetail.respInfo = tmpPlainJson.getString("returnMsg");
            }
            tmpRetDetail.transState = "payStatus+" + tmpPlainJson.getString("payStatus");
			tmpRetDetail.payInfoCode = tmpPlainJson.getString("returnCode");
		} catch (Exception ex) {
			ex.printStackTrace();
			tmpRetDetail.respCode = "-1";
			tmpRetDetail.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}

		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}

	public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception{
		RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
		RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();

		try {
			ActSetItem actItem = getBankConfig().getActSetItem(p_req1944.payAct, "");
			if (actItem != null) {
				SinglePayRecQueryRequest singleQryRequest = new SinglePayRecQueryRequest();
				singleQryRequest.setVersion("V5.1");
				singleQryRequest.setInputCharset(getCharset());
				singleQryRequest.setProductNo(getBankConfig().getMapValueByKey("productNoMapping", "singlePay"));
				singleQryRequest.setCustSeq(p_req1944.askNumber);
				
		        JSONObject tmpRespJson = sendToBank(JSONObject.toJSONString(singleQryRequest), actItem, bankFront.getUrl("singleRecPayQry"));
		        
		        JSONObject tmpPlainJson = new JSONObject();
	            if ("RSA".equals(tmpRespJson.get("encryptType"))) {
	            	tmpPlainJson = decryptByRSA(tmpRespJson, actItem);
					
                	tmpRetDetail.respCode = "0";
    				tmpRetDetail.respInfo = tmpPlainJson.getString("returnMsg");
    				tmpRetDetail.transAmt = tmpPlainJson.getString("orderAmount");
    				tmpRetDetail.transState = "payStatus+" + tmpPlainJson.get("payStatus");
    				tmpRetDetail.payInfoCode = tmpPlainJson.getString("returnCode");
	            } else {
	            	tmpPlainJson = JSONObject.parseObject((String)tmpRespJson.get("reqmsg"));
	            	
	                tmpRetDetail.respCode = "-1";
					tmpRetDetail.respInfo = tmpPlainJson.getString("returnMsg");
	            }
			} else {
				tmpRetDetail.respCode = "-1";
				tmpRetDetail.respInfo = "未找到账号:[" + p_req1944.payAct + "]商户配置信息!";
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			tmpRetDetail.respCode = "-1";
			tmpRetDetail.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}

		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}

	@Override
	public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
		return null;
	}
	
	@Override
	public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
		return null;
	}
}
